from sqlalchemy.inspection import inspect
from sqlalchemy.ext.declarative import declarative_base
from typing import Any, List, Union
import json


Base = declarative_base()

# from schema.baseResponse import PageResponse

def serialize_objects(obj_list: List[Any]) -> List[dict]:
    """
    Serialize a list of SQLAlchemy objects into a list of dictionaries.
    """
    if not obj_list:
        return []
    return [serialize_object(obj) for obj in obj_list]

def serialize_object(obj: Any) -> dict:
    """
    Serialize a single SQLAlchemy object into a dictionary.
    """
    return {c.key: getattr(obj, c.key) for c in inspect(obj).mapper.column_attrs}


def serializes(obj_or_list: Union[Any, List[Any]]) -> Union[dict, List[dict]]:
    """
    Serialize a single SQLAlchemy object or a list of SQLAlchemy objects into a dictionary or list of dictionaries.
    """
    if isinstance(obj_or_list, list):
        # 如果输入是列表，序列化列表中的每个对象
        return [{c.key: getattr(obj, c.key) for c in inspect(obj).mapper.column_attrs} for obj in obj_or_list]
    else:
        # 如果输入是单个对象，直接序列化该对象
        return {c.key: getattr(obj_or_list, c.key) for c in inspect(obj_or_list).mapper.column_attrs}


